package com.fsquare.email.service.message;

import java.util.Map;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.util.ByteArrayDataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.app.VelocityEngine;
import org.broadleafcommerce.common.email.domain.EmailTarget;
import org.broadleafcommerce.common.email.service.info.EmailInfo;
import org.broadleafcommerce.common.email.service.message.Attachment;
import org.broadleafcommerce.common.email.service.message.EmailPropertyType;
import org.broadleafcommerce.common.email.service.message.MessageCreator;
import org.broadleafcommerce.common.email.service.message.NullMessageCreator;
import org.broadleafcommerce.common.email.service.message.VelocityMessageCreator;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;

import com.fsquare.email.service.info.ExtEmailInfo;

public class ExtVelocityMessageCreator extends VelocityMessageCreator {

	private static final Log LOG = LogFactory.getLog(ExtVelocityMessageCreator.class);
    
	public ExtVelocityMessageCreator(VelocityEngine velocityEngine, JavaMailSender mailSender, Map<String, Object> additionalConfigItems) {
        super(velocityEngine, mailSender, additionalConfigItems)       ;
    }
    
    @Override
    public MimeMessagePreparator buildMimeMessagePreparator(final Map<String,Object> props) {
        MimeMessagePreparator preparator = new MimeMessagePreparator() {
            public void prepare(MimeMessage mimeMessage) throws Exception {
                EmailTarget emailUser = (EmailTarget) props.get(EmailPropertyType.USER.getType());
                ExtEmailInfo info = (ExtEmailInfo) props.get(EmailPropertyType.INFO.getType());
                MimeMessageHelper message = new MimeMessageHelper(mimeMessage, (info.getAttachments() != null && info.getAttachments().size() > 0));
                message.setTo(emailUser.getEmailAddress());
                
                if (info.getFromName() != null && !info.getFromName().equals("")) {
					InternetAddress fromAddress = new InternetAddress(info.getFromAddress(), info.getFromName());
					message.setFrom(fromAddress);
				}else {
					message.setFrom(info.getFromAddress());
				}
                
                message.setSubject(info.getSubject());
                if (emailUser.getBCCAddresses() != null && emailUser.getBCCAddresses().length > 0) {
                    message.setBcc(emailUser.getBCCAddresses());
                }
                if (emailUser.getCCAddresses() != null && emailUser.getCCAddresses().length > 0) {
                    message.setCc(emailUser.getCCAddresses());
                }
                String messageBody = info.getMessageBody();
                if (messageBody == null) {                  
                    messageBody = buildMessageBody(info, props);
                }
                message.setText(messageBody, true);
                for (Attachment attachment : info.getAttachments()) {
                    ByteArrayDataSource dataSource = new ByteArrayDataSource(attachment.getData(), attachment.getMimeType());
                    message.addAttachment(attachment.getFilename(), dataSource);
                }
            }
        };
        return preparator;
               
   }

}
